<!--- ========================================================================== --->
<!--- Revised _dataflow.cfm for one way form-flow starting from originator only  --->
<!--- by Maan 12062007 (part of Group Approval eTask application)                --->
<!--- ========================================================================== --->
<script>
function UpdateFormFlow() {
	document.frmBatchApp.action=document.frmBatchApp.action + "&edit=&SaveFF=&updateflow="
	document.frmBatchApp.submit()
}
</script>
<CFPARAM NAME="lValidRoute" DEFAULT="True">
<CFSET sWarn = "">
<!--- no data-flow if eforms doesnt have processcode attached in it and eforms table doesnt have routeid --->
<CFSET lHasUserRole="No">
<CFSET sPrevRcpt=client.chapa>
<CFMODULE TEMPLATE="#client.common_path#cfm_wswindow.cfm" TITLE="Form-Flow Related Information" SHOW="True">

<CFQUERY NAME="GetActs" DATASOURCE="#client.company_dsn#">
	SELECT *
	FROM CINFFACTIVITY
	WHERE
		(PROCESSCODE = '#GetData.PROCESSCODE#')
	ORDER BY ORDERID, DESCRIPTION
</CFQUERY>
<CFSET arrActivity = ArrayNew(1)>
<CFSET x = 0>
<CFLOOP QUERY="GetActs">
	<CFSET x = x + 1>
	<CFSET arrActivity[x] = ACTIVITYID> 
</CFLOOP>
<CFSET lIsRouted="No">
<CFIF isDefined("btnUpdateFF")>
	<CFSET lValidRoute  = True>
</CFIF>
<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH="100%">
	<CFIF NOT GetActs.RecordCount>
		<TR>
			<TD COLSPAN=3 CLASS="bold11">
				<FONT COLOR="#FF0000">
				This form cannot be routed, the attached process is invalid.<br>
				Please contact your iBOS/e Administrator.
				</FONT>
			</TD>
		</TR>
		<CFSET lValidRoute  = False>
	</CFIF>
	<CFSET z = 0>
	<CFOUTPUT QUERY="GetActs">
		<CFSET z = z + 1>
		<TR VALIGN="top">
		<TD WIDTH="50%">
			<CFSETTING ENABLECFOUTPUTONLY="Yes">
			<CFSET lUserRole="No">
			<CFSET sIntentedRec  = "">
			<CFSWITCH EXPRESSION="#lcase(GetActs.MAPPING)#">
				<!--- the originator is the recipient --->
				<CFCASE VALUE="originator">
					<CFIF GetActs.CurrentRow EQ 1>
						<CFSET sIntentedRec = "<FONT COLOR=RED>Invalid! The originator cannot be the recipient of the first activity.</FONT>">
						<CFSET lValidRoute  = False>
					<CFELSE>
						<CFSET sIntentedRec = "(" & client.chapa & ") " & client.myname>
					</CFIF>

					<CFSET sPrevRcpt=client.chapa>
				</CFCASE>
				<!--- extract the recipient through the unitcode --->
				<CFCASE VALUE="userrole">
					<!--- get unitcode to use --->
					<CFSET sUnitCode2Use=Trim(GetActs.UNITCODE)>
					<CFIF sUnitCode2Use IS "">
						<CFSET sUnitCode2Use="PERSONNELIDNO">
					</CFIF>

					<!--- get intended recipients --->
					<CFQUERY NAME="GetRecipients" DATASOURCE="#client.global_dsn#">
						SELECT USERGRPMEMBERSIDX, GUID FROM EGRGROLEINDEX, EGRGUSERMASTER
						WHERE
							EGRGUSERMASTER.USERID = EGRGROLEINDEX.USERGRPMEMBERSIDX AND
							USERGRPID_FK = '#GetActs.ROLECODE#'
					</CFQUERY>

					<!--- if sole individual in role, SIYA NA! --->
					<CFIF GetRecipients.RecordCount EQ 1>
						<CFMODULE TEMPLATE="#client.common_path#cfm_getuser.cfm" USERID="#GetRecipients.USERGRPMEMBERSIDX#">
						<CFIF ISFOUND>
							<CFSET sIntentedRec = sIntentedRec & "(" & PERID & ") " & PERNAME & "<BR>">
						</CFIF>
					<CFELSE>
						<!--- if two or more recipients, extract one with same unitcode as originator --->
						<CFSET sValidActivityID=Replace(GetActs.ACTIVITYID, "-", "", "ALL")>
						<CFSET lUserRole="Yes">
						<CFQUERY NAME="getdivision" DATASOURCE="#client.company_dsn#">
							SELECT #sUnitCode2Use#
							FROM CMFPA
							WHERE PERSONNELIDNO = '#client.chapa#'
						</CFQUERY>
						<CFSET sCode2Use="">
						<CFIF IsDefined("form.selActivity")>
							<CFLOOP LIST="#form.selActivity#" INDEX="y">
								<CFIF ListLen(y,"~") EQ 2>
									<CFIF ListGetAt(y,2,"~") EQ arrActivity[z]>
										<CFSET sCode2Use= ListGetAt(y,1,"~")>
										<CFIF sCode2Use IS NOT "">
											<CFQUERY DATASOURCE="#client.global_dsn#" NAME="overridemapping">
												SELECT USERID
												FROM EGRGUSERMASTER
												WHERE GUID = '#sCode2Use#'
											</CFQUERY>
											<CFSET sCode2Use=overridemapping.USERID>
											<CFSET lValidRoute  = True>
										</CFIF>
									</CFIF>
								<CFELSE>
									<CFSET sIntentedRec ="<FONT COLOR=red>Select approver. Originator should not be Approver.</FONT>">
									<CFSET lValidRoute  = False>
								</CFIF>
							</CFLOOP>
						</CFIF>
						<CFIF sCode2Use EQ "">
							<CFSET sIntentedRec ="">
						<CFELSE>
							<CFMODULE TEMPLATE="#client.common_path#cfm_getuser.cfm" USERID="#sCode2Use#">
							<CFIF PERID NEQ "#CLIENT.CHAPA#">
								<CFSET sIntentedRec = sIntentedRec & "(" & PERID & ") " & PERNAME & "<BR>">
								<CFSET sPrevRcpt=PERID>
							<CFELSE>
								<CFSET sIntentedRec ="<FONT COLOR=red>Select approver. Originator should not be Approver.</FONT>">
								<CFSET lValidRoute  = False>
							</CFIF>
						</CFIF>
					</CFIF>
				</CFCASE>
				<!--- the immediate superior is the recipient --->
				<CFDEFAULTCASE> <!--- superior --->
					<CFSET sNoCode = "">
					<CFLOOP QUERY="GetData2"> <!--- validate each applicant's immediate superior ... by Maan 01202008 01:37H--->
						<!--- get superior code --->
						<CFQUERY NAME="GetEmpCode" DATASOURCE="#client.company_dsn#">
							SELECT SUPERIORCODE, GUID, PERSONNELIDNO, LASTNAME #sqlCONCAT# ', ' #sqlCONCAT# FIRSTNAME #sqlCONCAT# ' ' #sqlCONCAT# MIDDLENAME AS NAME
							FROM CMFPA
							WHERE PERSONNELIDNO = '#PERSONNELIDNO#'
						</CFQUERY>
						<CFIF GetEmpCode.SUPERIORCODE NEQ "">
							<CFQUERY NAME="GetSupCode" DATASOURCE="#client.company_dsn#">
								SELECT GUID, PERSONNELIDNO, LASTNAME #sqlCONCAT# ', ' #sqlCONCAT# FIRSTNAME #sqlCONCAT# ' ' #sqlCONCAT# MIDDLENAME AS NAME
								FROM CMFPA
								WHERE PERSONNELIDNO = '#GetEmpCode.SUPERIORCODE#'
							</CFQUERY>
							<!--- is superior a valid user --->
							<CFIF GetSupCode.RecordCount>
								<CFQUERY NAME="IsExists" DATASOURCE="#client.global_dsn#">
									SELECT USERID
									FROM EGRGUSERMASTER
									WHERE
										GUID = '#GetSupCode.GUID#'
								</CFQUERY>
								<CFIF IsExists.RecordCount>
									<CFSET sIntentedRec = "<i>Recipients may vary</i>">
								<CFELSE>
									<CFSET sIntentedRec = "<FONT COLOR=RED>Invalid! The recipient is not an iBOS/e Users.</FONT>">
									<CFSET lValidRoute  = False>
								</CFIF>
							</CFIF>
						<CFELSE>
							<CFSET sIntentedRec = "<i>Recipients may vary</i>">
							<CFSET sNoCode = sNoCode & "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" & PERSONNELIDNO & "- " & GetEmpCode.NAME & "<br>">
						</CFIF>
					</CFLOOP>
					<CFIF sNoCode NEQ "">
						<CFSET lValidRoute  = False>
						<CFSET sWarn = "There has been a problem mapping superiorcode for the following:<br>" & sNoCode>
					</CFIF>
				</CFDEFAULTCASE>
			</CFSWITCH>
			<CFSETTING ENABLECFOUTPUTONLY="No">
			<!--- scheduled activities --->
			<TABLE BORDER=0 CELLPADDING=2 CELLSPACING=0 WIDTH="100%">
				<TR>
					<TD NOWRAP>Sequence No.</TD>
					<TD CLASS="bold" WIDTH="100%">#ORDERID#</TD>
				</TR>
				<TR>
					<TD>Description&nbsp;</TD>
					<TD CLASS="bold">#DESCRIPTION#</TD>
				</TR>
				<TR VALIGN="top">
					<TD>Recipient/s</TD>
					<TD CLASS="bold">
						<CFIF sIntentedRec EQ "">
							<FONT COLOR="red">No intended recipient(s).</FONT>
							<CFSET lValidRoute  = False>
						<CFELSE>
							#sIntentedRec#
						</CFIF>
					</TD>
				</TR>
				<CFIF #len(trim(ACTIONCODE))# NEQ 0>
					<CFQUERY NAME="GetAction" DATASOURCE="#client.company_dsn#">
						SELECT DESCRIPTION
						FROM CLKACTIONS
						WHERE
							(ACTIONCODE = '#ACTIONCODE#')
					</CFQUERY>
				</CFIF>
				<CFIF lUserRole AND NOT lIsRouted>
					<CFSET lHasUserRole="Yes">
					<CFSET lstGUID=quotedValuelist(GetRecipients.GUID, ',')>
					<CFIF lstGUID EQ "">
						<CFSET lstGUID="''">
					</CFIF>
					<CFQUERY NAME="getdivision" DATASOURCE="#client.company_dsn#">
						SELECT LASTNAME, FIRSTNAME, MIDDLENAME, GUID
						FROM CMFPA
						WHERE GUID IN (#preserveSingleQuotes(lstGUID)#)
						ORDER BY LASTNAME,FIRSTNAME,MIDDLENAME
					</CFQUERY>

					<tr>
						<td nowrap>Override Default Recipient:</td>
						<td><select name="selActivity">
								<option value=" ">--Select new recipient--
								<CFLOOP QUERY="getdivision">
									<option value="#getdivision.GUID#~#arrActivity[z]#">#getdivision.LASTNAME#, #getdivision.FIRSTNAME# #getdivision.MIDDLENAME#
								</CFLOOP>
							</select>
							<input type="hidden" name="hidselActivity" value="#sCode2Use#">
						</td>
					</tr>
				</CFIF>
			</TABLE>
		</TD>
		<TD BGCOLOR="##808080"></TD>
		<TD WIDTH="50%">
		</TD>
		</TR>
		<CFIF lValidRoute AND lUserRole><!--- insert to tmp table replica of ECMFUSRDEFINEROLE --->
			<CFLOOP LIST="#form.selActivity#" INDEX="y">
				<CFIF ListLen(y,"~") EQ 2>
					<CFIF ListGetAt(y,2,"~") EQ arrActivity[z]>
						<CFQUERY NAME="deletetmp" DATASOURCE="#client.company_dsn#">
							DELETE FROM ECINBATCHROUTEROLE
							WHERE 	(BATCHID = '#getform.formcode#') AND 
								(DOCNUMBER = '#url.docnumber#') AND
								(ACTIVITYID = '#arrActivity[z]#')
						</CFQUERY>
						<CFQUERY DATASOURCE="#client.global_dsn#" NAME="qryUserid">
							SELECT USERID
							FROM EGRGUSERMASTER
							WHERE GUID = '#ListGetAt(y,1,"~")#'
						</CFQUERY>
						<CFQUERY NAME="insert2tmp" DATASOURCE="#client.company_dsn#">
							INSERT INTO ECINBATCHROUTEROLE (BATCHID, DOCNUMBER, ACTIVITYID, USERID)
							VALUES ('#getform.formcode#',
								'#url.docnumber#',
								'#arrActivity[z]#',
								'#qryUserid.USERID#')
						</CFQUERY>
					</CFIF>
				</CFIF>
			</CFLOOP>
		<CFELSEIF lUserRole>
			<CFQUERY NAME="deletetmp" DATASOURCE="#client.company_dsn#">
				DELETE FROM ECINBATCHROUTEROLE
				WHERE 	(BATCHID = '#getform.formcode#') AND 
					(DOCNUMBER = '#url.docnumber#') AND
					(ACTIVITYID = '#arrActivity[z]#')
			</CFQUERY>
		</CFIF>
		<CFIF CurrentRow NEQ RecordCount>
			<TR><TD COLSPAN=3><HR></TD></TR>
		</CFIF>
	</CFOUTPUT>
	<CFOUTPUT>
		<tr><td colspan=3><HR></td></tr>
		<TR>
			<TD colspan=3><font color="red"><b>#sWarn#</b></TD>
		</TR>
		<tr>	
			<td>
				<CFIF lValidRoute>
					<input type="submit" value="Route" name="btnRouteForms">&nbsp;
				</CFIF>
				<CFIF lHasUserRole>
					<input type="submit" value="Update Form-Flow" name="btnUpdateFF">&nbsp;
				</CFIF>
				<input type="button" value="Back" name="btnBack" onclick="javascript: history.back();">&nbsp;
			</td><td colspan=2></td>
		</tr>
	</CFOUTPUT>
	
</TABLE>
<CFMODULE TEMPLATE="#client.common_path#cfm_wswindow.cfm" TOP="False">
